package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.constant.QiniuConstant;
import cn.huiyunche.base.service.framework.utils.HYCUtils;
import cn.huiyunche.base.service.framework.utils.QiniuUtils;
import cn.huiyunche.base.service.interfaces.VehicleBrandService;
import cn.huiyunche.base.service.mappers.BVehicleBrandMapper;
import cn.huiyunche.base.service.model.BVehicleBrand;
import cn.huiyunche.base.service.model.BVehicleBrandExample;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehicleBrandServiceImpl implements VehicleBrandService {

    private static final Logger logger = LoggerFactory.getLogger(VehicleBrandServiceImpl.class);
    @Autowired
    private BVehicleBrandMapper bVehicleBrandMapper = null;

    @Override
    public List<BVehicleBrand> selectVehicleBrand() throws Exception {
        BVehicleBrandExample search = new BVehicleBrandExample();
        search.setOrderByClause("brand_prefix asc");
        List<BVehicleBrand> vehicleBrands = this.getBVehicleBrandMapper().selectByExample(search);
        for (BVehicleBrand brand : vehicleBrands) {
            String strUrl = QiniuUtils.generateDownloadTicket(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_LOGO_BUCKET), brand.getBrandLogo(), "",
                    "200", "200");
            brand.setBrandLogo(strUrl);
        }
        return vehicleBrands;
    }

    @Override
    public BVehicleBrand selectLikeName(String name) throws Exception {
        BVehicleBrandExample search = new BVehicleBrandExample();
        BVehicleBrandExample.Criteria brands = search.createCriteria();
        brands.andBrandNameLike(name);
        List<BVehicleBrand> vehicleBrands = this.getBVehicleBrandMapper().selectByExample(search);
        logger.info("select vehicle brand name  like:{} ,result: {}", name, JSON.toJSON(vehicleBrands));
        if (vehicleBrands == null || vehicleBrands.size() == 0) {
            return null;
        }
        return vehicleBrands.get(0);
    }


    private BVehicleBrandMapper getBVehicleBrandMapper() {
        return this.bVehicleBrandMapper;
    }
}
